package com.adcc.ags.utility.acars620;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ACARS620报文验证类
 */
public final class ACARS620Verification {

    // 620报文char3
    private static final String MSG_TAG_CHAR3 = "\003";

    /**
     * 报文校验，包括正则表达式校验和自由文非法字符检验
     * @param rawMsg
     * @return true：校验成功；false：校验失败
     * */
    public static boolean verify(String rawMsg) {
        // 正则表达式校验
        Pattern p = Pattern.compile(Constant.REGEX_ACARS620);
        Matcher m = p.matcher(rawMsg);
        if (!m.matches()) {
            return false;
        }

        // 自由文字符校验
        int freeTextBegin = rawMsg.indexOf("\n-  ");
        if (freeTextBegin < 0) {
            freeTextBegin = rawMsg.indexOf("\n- ");
        }
        int freeTextEnd = rawMsg.lastIndexOf(MSG_TAG_CHAR3);
        if (freeTextBegin > 0) {
            String freeText = rawMsg.substring(freeTextBegin, freeTextEnd);
            if (!verifyChar(freeText.toCharArray())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 报文校验，包括正则表达式校验、长度校验和自由文非法字符检验
     * @param rawMsg
     * @param length
     * @return true：校验成功；false：校验失败
     * */
    public static boolean verify(String rawMsg, int length) {
        // 正则表达式校验
        Pattern p = Pattern.compile(Constant.REGEX_ACARS620);
        Matcher m = p.matcher(rawMsg);
        if (!m.matches()) {
            return false;
        }

        // 长度校验
        if (!verifyLength(rawMsg, length)) {
            return false;
        }

        // 自由文字符校验
        int freeTextBegin = rawMsg.indexOf("\n-  ");
        if (freeTextBegin < 0) {
            freeTextBegin = rawMsg.indexOf("\n- ");
        }
        int freeTextEnd = rawMsg.lastIndexOf(MSG_TAG_CHAR3);
        if (freeTextBegin > 0) {
            String freeText = rawMsg.substring(freeTextBegin, freeTextEnd);
            if (!verifyChar(freeText.toCharArray())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 校验message的长度
     * */
    private static boolean verifyLength(String message, int length) {
        if (length < message.length()) {
            return false;
        }
        return true;
    }

    /**
     * 校验message的每个字符
     * */
    private static boolean verifyChar(char[] chars) {
        for (int i=0; i<chars.length; i++) {
            if (!verifyChar(chars[i])) {
                return false;
            }
        }
        return true;
    }

    /**
     * 单个字符校验
     * */
    private static boolean verifyChar(int ch) {
        if (ch < 0 || ch > 127) {
            return false;
        }
        // reject if it equals to 000 or 001, unless \x0a and \0x0d (CR, LF)
        int h765 = (ch >> 4) & 7;
        if (h765 == 0 || h765 == 1) {
            if (ch == 0x0a || ch == 0x0d) {
                return true;
            }
            return false;
        }
        // \x7f  (DEL) is reserved, not allowed for now
        return ch != 0x7f;
    }
}
